package com.panda.takeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panda.takeaway.auth.util.SecurityUtil;
import com.panda.takeaway.contant.SqlConstant;
import com.panda.takeaway.dao.ShopMapper;
import com.panda.takeaway.entity.dto.CommonResult;
import com.panda.takeaway.entity.po.Shop;
import com.panda.takeaway.entity.vo.BySchQueryVo;
import com.panda.takeaway.entity.vo.PageInfoVo;
import com.panda.takeaway.entity.vo.ShopInfoVo;
import com.panda.takeaway.entity.dto.PageBean;
import com.panda.takeaway.service.ShopService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author John
 * @create 2021-08-09-15:24
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private ShopMapper shopMapper;

    @Override
    public CommonResult<?> addShop(ShopInfoVo shopInfoVo) {


        Assert.notNull(shopInfoVo, "shop对象为null");

        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SecurityUtil.getUserId());
        if (shopMapper.selectByMap(map).size() > 0) {
            return CommonResult.operateFailWithMessage("你已注册过一家店铺");
        }

        Shop shop = new Shop();
        BeanUtils.copyProperties(shopInfoVo, shop);
        //设置店主id
        shop.setUserId(SecurityUtil.getUserId());
        //自动生成
        shop.setId(null);
        shop.setShopLabel(shopInfoVo.getShopLabel().toString());


        if (shopMapper.insert(shop) > 0) {
            return CommonResult.operateSuccess();
        }


        return CommonResult.operateFailWithMessage("系统繁忙，请稍后重试！");


    }

    @Override
    public CommonResult<?> updateShop(ShopInfoVo shopInfoVo) {


        if (shopMapper.selectById(shopInfoVo.getShopId()) == null) {

            return CommonResult.operateFailWithMessage("修改店铺不存在");

        }

        Shop shop = new Shop();
        BeanUtils.copyProperties(shopInfoVo, shop);
        shop.setId(shopInfoVo.getShopId());
        shop.setShopLabel(Arrays.toString(shopInfoVo.getShopLabel()));


        //避免被恶意修改店铺销量
        shop.setDaySales(null);

        if (shopMapper.updateById(shop) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage("系统繁忙，修改店铺信息失败！");
    }

    @Override
    public CommonResult<?> queryAllShops(PageInfoVo pageInfoVo) {



        if (!(pageInfoVo.getPage() > 0)) {
            return CommonResult.operateFailWithMessage("页数必须大于零");
        }

        if (pageInfoVo.getSize() == null || !(pageInfoVo.getSize() > 0)) {
            pageInfoVo.setSize(SqlConstant.PAGE_LIMIT);
        }

        QueryWrapper<Shop> wrapper = new QueryWrapper<Shop>()
                .orderByAsc("yesterday_sales");

        Page<Shop> page = new Page<>(pageInfoVo.getPage(), pageInfoVo.getSize());
        shopMapper.selectPage(page, wrapper);
        List<Shop> shopList = page.getRecords();
        Long totalCount = Long.valueOf(shopMapper.selectCount(null));
        Long totalPage = (totalCount + pageInfoVo.getSize() - 1) / pageInfoVo.getSize();
        PageBean<Shop> shopPage = new PageBean<>(pageInfoVo, totalCount, totalPage, shopList);

        if (shopList.size() == 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateSuccess(shopPage);
    }

    @Override
    public CommonResult<?> queryShopsBySchool(BySchQueryVo bySchQueryVo) {


        if (!(bySchQueryVo.getPage() > 0)) {
            return CommonResult.operateFailWithMessage("页数必须大于零");
        }

        if (bySchQueryVo.getSize() == null || !(bySchQueryVo.getSize() > 0)) {
            bySchQueryVo.setSize(SqlConstant.PAGE_LIMIT);
        }

        int offset = (bySchQueryVo.getPage() - 1) * bySchQueryVo.getSize();
        List<Shop> shopList = shopMapper.queryShopsBySchool(bySchQueryVo.getSchoolId(), SqlConstant.PAGE_LIMIT, offset);

        Long totalCount = Long.valueOf(shopMapper.selectCount(null));
        Long totalPage = (totalCount + bySchQueryVo.getSize() - 1) / bySchQueryVo.getSize();

        PageBean<Shop> shopPage = new PageBean<>(bySchQueryVo, totalCount, totalPage, shopList);

        if (shopList.size() == 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateSuccess(shopPage);
    }


}
